package com.swh.controller;

import com.swh.entity.Dept;
import com.swh.servic.DeptService;
import com.swh.servic.UserServic;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/23 16:57
 * @Version: 1.0
 */
@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptService();
    private UserServic userServic = new UserServic();

    //查询所有部门
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Map<String, Object>> deptMapList = deptService.findAll();

        for (Map<String, Object> deptMap : deptMapList) {
            long deptId = Long.parseLong(deptMap.get("id").toString());

            List<Map<String, Object>> userMapList = userServic.findUserByDeptId(deptId);

            deptMap.put("userMapList", userMapList);
        }

        request.setAttribute("deptMapList", deptMapList);
        request.getRequestDispatcher("/html/department.jsp").forward(request, response);
    }

    public void findDeptAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Dept> deptList = deptService.findDeptAll();

        writeObjToString(response,deptList);
    }

}
